package com.cndongtai.service.report;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cndongtai.dao.impl.ChargeCountDao;
import com.cndongtai.dao.impl.DtuDao;
import com.cndongtai.dao.impl.LightChargeCountDao;
import com.cndongtai.dao.impl.LightConfigDao;
import com.cndongtai.dao.impl.LightCurrentRecordDao;
import com.cndongtai.dao.impl.LightDao;
import com.cndongtai.dao.impl.LightStatusRecordDao;
import com.cndongtai.dao.impl.UserDao;
import com.cndongtai.po.Dtu;
import com.cndongtai.po.Light;
import com.cndongtai.po.LightChargeCount;
import com.cndongtai.po.LightConfig;
import com.cndongtai.po.LightCurrentRecord;
import com.cndongtai.po.LightStatusRecord;
import com.cndongtai.po.User;
import com.cndongtai.util.DateUtil;
import com.cndongtai.vo.LightChargeCountBean;
import com.cndongtai.vo.LightCurrentBean;
import com.cndongtai.vo.LightInfoBean;
import com.cndongtai.vo.LightStatusBean;
import com.cndongtai.vo.PageForm;
import com.cndongtai.vo.UserChargeCountBean;

@Service("reportService")
public class ReportService {

	@Resource(name="lightStatusRecordDao")
	private LightStatusRecordDao lightStatusRecordDao;
	
	@Resource(name="lightCurrentRecordDao")
	private LightCurrentRecordDao lightCurrentRecordDao;
	
	@Resource(name="lightDao")
	private LightDao lightDao;
	
	@Resource(name="dtuDao")
	private DtuDao dtuDao;
	
	@Resource(name="userDao")
	private UserDao userDao;
	
	@Resource(name="lightConfigDao")
	private LightConfigDao lightConfigDao;
	
	@Resource(name="lightChargeCountDao")
	private LightChargeCountDao lightChargeCountDao;
	
	@Resource(name="chargeCountDao")
	private ChargeCountDao chargeCountDao;
	
	public PageForm<LightStatusBean> queryStatusListPage(PageForm<LightStatusBean> page) {
//		long time1 = System.currentTimeMillis();
		// 查询总数
		int totalSize = lightStatusRecordDao.queryLightStatusRecordListCount(page.getConditionMap());
//		page.setPageSize(23);
//		long time2 = System.currentTimeMillis();
		if(totalSize>0) {
			page.setTotalSize(totalSize);
			List<LightStatusRecord> lightStatusRecordList = lightStatusRecordDao.queryLightStatusRecordListPage(page.getConditionMap());
//			long time3 = System.currentTimeMillis();
			page.setDataList(convertLightStatusRecordToLightStatusBean(lightStatusRecordList));
//			long time4 = System.currentTimeMillis();
//			System.out.println("time1==>" + (time2-time1));
//			System.out.println("time2==>" + (time3-time2));
//			System.out.println("time3==>" + (time4-time3));
		}else {
			page.setDataList(new ArrayList<LightStatusBean>());
		}
		
		return page;
	}
	
	private List<LightStatusBean> convertLightStatusRecordToLightStatusBean(List<LightStatusRecord> lightStatusRecordList) {
		if(lightStatusRecordList==null || lightStatusRecordList.size()==0) {
			return null;
		}
		
		List<LightStatusBean> lightStatusBeanList = new ArrayList<LightStatusBean>();
		
		for(LightStatusRecord statusRecord : lightStatusRecordList) {
			LightStatusBean statusBean = new LightStatusBean();
			
			statusBean.setRecordId(statusRecord.getRecordId());
			statusBean.setCollectTime(DateUtil.timeFormat(statusRecord.getCollectTime()));
			statusBean.setStatusCode(statusRecord.getStatusCode());
			statusBean.setStatusType("");//根据status获取描述
			statusBean.setTemperature(statusRecord.getTemperature()/10.0+"");
			statusBean.setSolarPanelVol(statusRecord.getSolarPanelVol()/10.0+"");
			statusBean.setBatteryVol(statusRecord.getBatteryVol()/10.0+"");
			statusBean.setBatteryVolLevel("5");
			
			LightInfoBean lightInfo = lightDao.queryLightInfoById(statusRecord.getLightId());
			
			// 获取路灯信息
			if(lightInfo==null) {
				System.out.println(statusRecord.getLightId()   + "   该设备查不到=====");
				continue;
			}
						
			LightConfig lightConfig = lightInfo.getConfig();//lightConfigDao.queryLightConfigByLightId(statusRecord.getLightId());
			
			int topVol = 120;
			int lowVol = 90;
			if(lightConfig!=null) {
				if(lightConfig.getTopVol()!=null) {
					topVol = lightConfig.getTopVol();
				}
				
				if(lightConfig.getLowVol()!=null) {
					lowVol = lightConfig.getLowVol();
				}
			}
			
			/*if(lowVol>=statusRecord.getBatteryVol()) {
				// 欠压
				statusBean.setBatteryVolLevel("1");
			}else if(topVol<=statusRecord.getBatteryVol() || topVol<=lowVol) {
				statusBean.setBatteryVolLevel("10");
			}else {
				int i = (statusRecord.getBatteryVol()-lowVol)*10/(topVol-lowVol);
				statusBean.setBatteryVolLevel(i+"");
			}*/
			
			if(statusRecord.getBatteryVol()<=95) {
				// 欠压
				statusBean.setBatteryVolLevel("0");
			}else if(statusRecord.getBatteryVol()<=101.7) {
				statusBean.setBatteryVolLevel("1");
			}else if(statusRecord.getBatteryVol()<=103.9) {
				statusBean.setBatteryVolLevel("2");
			}else if(statusRecord.getBatteryVol()<=105.2) {
				statusBean.setBatteryVolLevel("3");
			}else if(statusRecord.getBatteryVol()<=106.3) {
				statusBean.setBatteryVolLevel("4");
			}else if(statusRecord.getBatteryVol()<=107.5) {
				statusBean.setBatteryVolLevel("5");
			}else if(statusRecord.getBatteryVol()<=109.5) {
				statusBean.setBatteryVolLevel("6");
			}else if(statusRecord.getBatteryVol()<=112.3) {
				statusBean.setBatteryVolLevel("7");
			}else if(statusRecord.getBatteryVol()<=115.6) {
				statusBean.setBatteryVolLevel("8");
			}else if(statusRecord.getBatteryVol()<=119) {
				statusBean.setBatteryVolLevel("9");
			}else if(statusRecord.getBatteryVol()<=126) {
				statusBean.setBatteryVolLevel("10");
			}else {
				statusBean.setBatteryVolLevel("10");
			}
			
			statusBean.setChargeCurrent(statusRecord.getChargeCurrent());
			statusBean.setWorkVol(statusRecord.getWorkVol()/10.0+"");
			statusBean.setWorkCurrent(statusRecord.getWorkCurrent());
			statusBean.setChargeCount(statusRecord.getChargeCount()/10.0+"");
			statusBean.setWorkCount(statusRecord.getWorkCount()/10.0+"");
			
			Dtu dtu = lightInfo.getDtu();//dtuDao.queryDtuById(light.getDtuId());
			if(dtu==null || dtu.getPanId()==null) {
				System.out.println(statusRecord.getLightId()   + "   该设备查不到Dtu=====");
				continue;
			}
			
			statusBean.setPanId(dtu.getPanId());
			statusBean.setAddressName(lightInfo.getAddressName());
			statusBean.setLightNum(lightInfo.getLightNum());
			
			lightStatusBeanList.add(statusBean);
		}
		
		return lightStatusBeanList;
	}
	
	public List<LightStatusBean> queryStatusList(Long userId, String lightNum, String panId) {
		List<LightStatusRecord> lightStatusRecordList = lightStatusRecordDao.queryLightStatusRecordList(userId, lightNum, panId);
		return convertLightStatusRecordToLightStatusBean(lightStatusRecordList);
	}
	
	public LightStatusBean queryLightLatestStatus(Long userId, String lightId) {
		LightStatusRecord statusRecord = lightStatusRecordDao.queryLightLatestStatusRecord(userId, lightId);
		if(statusRecord==null) {
			return null;
		}
		
		LightStatusBean statusBean = new LightStatusBean();
		
		statusBean.setRecordId(statusRecord.getRecordId());
		statusBean.setCollectTime(DateUtil.timeFormat(statusRecord.getCollectTime()));
		statusBean.setStatusCode(statusRecord.getStatusCode());
		statusBean.setStatusType("");//根据status获取描述
		statusBean.setTemperature(statusRecord.getTemperature()/10.0+"");
		statusBean.setSolarPanelVol(statusRecord.getSolarPanelVol()/10.0+"");
		statusBean.setBatteryVol(statusRecord.getBatteryVol()/10.0+"");
		statusBean.setBatteryVolLevel("5");
		
		LightConfig lightConfig = lightConfigDao.queryLightConfigByLightId(statusRecord.getLightId());
		
		int topVol = 120;
		int lowVol = 90;
		if(lightConfig!=null) {
			topVol = lightConfig.getTopVol();
			lowVol = lightConfig.getLowVol();
		}
		
		/*if(lowVol>=statusRecord.getBatteryVol()) {
			// 欠压
			statusBean.setBatteryVolLevel("1");
		}else if(topVol<=statusRecord.getBatteryVol() || topVol<=lowVol) {
			statusBean.setBatteryVolLevel("10");
		}else {
			int i = (statusRecord.getBatteryVol()-lowVol)*10/(topVol-lowVol);
			statusBean.setBatteryVolLevel(i+"");
		}*/
		
		if(statusRecord.getBatteryVol()<=95) {
			// 欠压
			statusBean.setBatteryVolLevel("0");
		}else if(statusRecord.getBatteryVol()<=101.7) {
			statusBean.setBatteryVolLevel("1");
		}else if(statusRecord.getBatteryVol()<=103.9) {
			statusBean.setBatteryVolLevel("2");
		}else if(statusRecord.getBatteryVol()<=105.2) {
			statusBean.setBatteryVolLevel("3");
		}else if(statusRecord.getBatteryVol()<=106.3) {
			statusBean.setBatteryVolLevel("4");
		}else if(statusRecord.getBatteryVol()<=107.5) {
			statusBean.setBatteryVolLevel("5");
		}else if(statusRecord.getBatteryVol()<=109.5) {
			statusBean.setBatteryVolLevel("6");
		}else if(statusRecord.getBatteryVol()<=112.3) {
			statusBean.setBatteryVolLevel("7");
		}else if(statusRecord.getBatteryVol()<=115.6) {
			statusBean.setBatteryVolLevel("8");
		}else if(statusRecord.getBatteryVol()<=119) {
			statusBean.setBatteryVolLevel("9");
		}else if(statusRecord.getBatteryVol()<=126) {
			statusBean.setBatteryVolLevel("10");
		}else {
			statusBean.setBatteryVolLevel("10");
		}
		
		statusBean.setChargeCurrent(statusRecord.getChargeCurrent());
		statusBean.setWorkVol(statusRecord.getWorkVol()/10.0+"");
		statusBean.setWorkCurrent(statusRecord.getWorkCurrent());
		statusBean.setChargeCount(statusRecord.getChargeCount()/10.0+"");
		statusBean.setWorkCount(statusRecord.getWorkCount()/10.0+"");
		
		// 获取路灯信息
		Light light = lightDao.queryLightById(statusRecord.getLightId());
		if(light==null) {
			System.out.println(statusRecord.getLightId()   + "   该设备查不到=====");
			return null;
		}
		
		Dtu dtu = dtuDao.queryDtuById(light.getDtuId());
		if(dtu==null) {
			return null;
		}
		
		statusBean.setPanId(dtu.getPanId());
		statusBean.setAddressName(light.getAddressName());
		statusBean.setLightNum(light.getLightNum());
			
		return statusBean;
	}
	
	public List<LightStatusBean> queryFaultList(Long userId) {
		List<LightStatusRecord> lightStatusRecordList = lightStatusRecordDao.queryLightStatusRecordList(userId,null,null);
		if(lightStatusRecordList==null || lightStatusRecordList.size()==0) {
			return null;
		}
		
		List<LightStatusBean> lightStatusBeanList = new ArrayList<LightStatusBean>();
		
		for(LightStatusRecord statusRecord : lightStatusRecordList) {
			LightStatusBean statusBean = new LightStatusBean();
			
			statusBean.setRecordId(statusRecord.getRecordId());
			statusBean.setCollectTime(DateUtil.timeFormat(statusRecord.getCollectTime()));
			statusBean.setStatusCode(statusRecord.getStatusCode());
			
			//根据status获取描述
			if("A1".equals(statusRecord.getStatusCode())) {
				statusBean.setStatusType("欠压保护");
			}else if("A2".equals(statusRecord.getStatusCode())) {
				statusBean.setStatusType("开路保护");
			}else if("A3".equals(statusRecord.getStatusCode())) {
				statusBean.setStatusType("过功率");
			}else if("A4".equals(statusRecord.getStatusCode())) {
				statusBean.setStatusType("峰值配置错误");
			}else if("A5".equals(statusRecord.getStatusCode())) {
				statusBean.setStatusType("电池接反");
			}else if("A6".equals(statusRecord.getStatusCode())) {
				statusBean.setStatusType("控制器型号错误");
			}else if("A7".equals(statusRecord.getStatusCode())) {
				statusBean.setStatusType("Zigbee模块异常");
			}else {
				continue;
			}
			
			statusBean.setTemperature(statusRecord.getTemperature()/10.0+"");
			statusBean.setSolarPanelVol(statusRecord.getSolarPanelVol()/10.0+"");
			statusBean.setBatteryVol(statusRecord.getBatteryVol()/10.0+"");
			statusBean.setChargeCurrent(statusRecord.getChargeCurrent());
			statusBean.setWorkVol(statusRecord.getWorkVol()/10.0+"");
			statusBean.setWorkCurrent(statusRecord.getWorkCurrent());
			statusBean.setChargeCount(statusRecord.getChargeCount()/10.0+"");
			statusBean.setWorkCount(statusRecord.getWorkCount()/10.0+"");
			
			// 获取路灯信息
			Light light = lightDao.queryLightById(statusRecord.getLightId());
			if(light==null) {
				System.out.println(statusRecord.getLightId()   + "   该设备查不到=====");
				continue;
			}
			
			statusBean.setAddressName(light.getAddressName());
			statusBean.setLightNum(light.getLightNum());
			
			// 获取路灯所有人
			Dtu dtu = dtuDao.queryDtuById(light.getDtuId());
			if(dtu!=null) {
				User userInfo = userDao.queryByUserId(dtu.getCreateBy());
				if(userInfo!=null) {
					statusBean.setMobile(userInfo.getMobile());
				}
			}
			
			lightStatusBeanList.add(statusBean);
		}
		
		return lightStatusBeanList;
	}
	
	public List<LightCurrentBean> queryCurrentList(Long userId) {
		List<LightCurrentRecord> lightCurrentRecordList = lightCurrentRecordDao.queryLightCurrentList(userId);
		if(lightCurrentRecordList==null || lightCurrentRecordList.size()==0) {
			return null;
		}
		
		List<LightCurrentBean> lightCurrentBeanList = new ArrayList<LightCurrentBean>();
		for(LightCurrentRecord record : lightCurrentRecordList) {
			LightCurrentBean bean = new LightCurrentBean();
			
			bean.setRecordId(record.getRecordId());
			bean.setLightId(record.getLightId());
			bean.setCollectTime(DateUtil.timeFormat(record.getCollectTime()));
			bean.setCurrentType(record.getCurrentType());
			bean.setWorkCurrent(record.getWorkCurrent());
			
			// 获取路灯信息
			Light light = lightDao.queryLightById(record.getLightId());
			bean.setLightNum(light.getLightNum());
			
			lightCurrentBeanList.add(bean);
		}
		
		return lightCurrentBeanList;
	}
	
	public String[] queryTemperatureData(String startTime, String endTime, Long lightId) {
		String[] result = new String[2];
		List<LightStatusRecord> statusList = lightStatusRecordDao.queryLightStatusRecordListByLightId(startTime, endTime, lightId);
		
		if(statusList==null || statusList.size()==0) {
			result[0] = "[0]";
			result[1] = "[\"2019\"]";
			return result;
		}
		
		Map<String, List<LightStatusRecord>> data = new HashMap<String, List<LightStatusRecord>>();
		
		List<String> keyList = new ArrayList<String>();
		// 数据分类
		for(LightStatusRecord status:statusList) {
			String key = DateUtil.timeFormat(status.getCollectTime());
			if(data.containsKey(key)) {
				data.get(key).add(status);
			}else {
				List<LightStatusRecord> recordList = new ArrayList<LightStatusRecord>();
				recordList.add(status);
				data.put(key, recordList);
				keyList.add(key);
			}
		}
		
		StringBuffer xData = new StringBuffer();
		xData.append("[");
		// 提取Y坐标
		StringBuffer yData = new StringBuffer();
		yData.append("[");
		// 提取X坐标
		for(String dateStr : keyList) {
			xData.append("'"+dateStr+"',");
			
			List<LightStatusRecord> recordList = data.get(dateStr);
			int temperature = 0;
			for(LightStatusRecord record : recordList) {
				temperature +=record.getTemperature();
			}
			
			yData.append(temperature/recordList.size()/10+",");
		}
		
		xData.deleteCharAt(xData.length()-1);
		xData.append("]");
		
		yData.deleteCharAt(yData.length()-1);
		yData.append("]");
		
		result[0] = yData.toString();
		result[1] = xData.toString();
		return result;
	}
	
	
	public String[] queryBatteryVolData(String startTime, String endTime, Long lightId) {
		String[] result = new String[2];
		List<LightStatusRecord> statusList = lightStatusRecordDao.queryLightStatusRecordListByLightId(startTime, endTime, lightId);
		
		if(statusList==null || statusList.size()==0) {
			result[0] = "[0]";
			result[1] = "[\"2019\"]";
			return result;
		}
		
		Map<String, List<LightStatusRecord>> data = new HashMap<String, List<LightStatusRecord>>();
		
		List<String> keyList = new ArrayList<String>();
		// 数据分类
		/*for(LightStatusRecord status:statusList) {
			String key = DateUtil.timeShortFormat(status.getCollectTime());
			if(data.containsKey(key)) {
				data.get(key).add(status);
			}else {
				List<LightStatusRecord> recordList = new ArrayList<LightStatusRecord>();
				recordList.add(status);
				data.put(key, recordList);
				keyList.add(key);
			}
		}*/
		
		for(LightStatusRecord status:statusList) {
			String key = DateUtil.timeFormat(status.getCollectTime());
			if(data.containsKey(key)) {
				data.get(key).add(status);
			}else {
				List<LightStatusRecord> recordList = new ArrayList<LightStatusRecord>();
				recordList.add(status);
				data.put(key, recordList);
				keyList.add(key);
			}
		}
		
		StringBuffer xData = new StringBuffer();
		xData.append("[");
		// 提取Y坐标
		StringBuffer yData = new StringBuffer();
		yData.append("[");
		// 提取X坐标
		for(String dateStr : keyList) {
			xData.append("'"+dateStr+"',");
			
			List<LightStatusRecord> recordList = data.get(dateStr);
			int batteryVol = 0;
			for(LightStatusRecord record : recordList) {
				batteryVol +=record.getBatteryVol();
			}
			
			yData.append(batteryVol/recordList.size()/10+",");
		}
		
		xData.deleteCharAt(xData.length()-1);
		xData.append("]");
		
		yData.deleteCharAt(yData.length()-1);
		yData.append("]");
		
		result[0] = yData.toString();
		result[1] = xData.toString();
		return result;
	}
	
	public String[] queryBatteryQuantityData(String startTime, String endTime, Long lightId) {
		String[] result = new String[2];
		List<LightStatusRecord> statusList = lightStatusRecordDao.queryLightStatusRecordListByLightId(startTime, endTime, lightId);
		
		if(statusList==null || statusList.size()==0) {
			result[0] = "[0]";
			result[1] = "[\"2019\"]";
			return result;
		}
		
		Map<String, List<LightStatusRecord>> data = new HashMap<String, List<LightStatusRecord>>();
		
		List<String> keyList = new ArrayList<String>();
		// 数据分类
		for(LightStatusRecord status:statusList) {
			String key = DateUtil.timeFormatH(status.getCollectTime());
			if(data.containsKey(key)) {
				data.get(key).add(status);
			}else {
				List<LightStatusRecord> recordList = new ArrayList<LightStatusRecord>();
				recordList.add(status);
				data.put(key, recordList);
				keyList.add(key);
			}
		}
		
		StringBuffer xData = new StringBuffer();
		xData.append("[");
		// 提取Y坐标
		StringBuffer yData = new StringBuffer();
		yData.append("[");
		
		/*LightConfig lightConfig = lightConfigDao.queryLightConfigByLightId(lightId.intValue());
		
		int topVol = 120;
		int lowVol = 90;
		if(lightConfig!=null) {
			topVol = lightConfig.getTopVol();
			lowVol = lightConfig.getLowVol();
		}*/
		
		// 提取X坐标
		for(String dateStr : keyList) {
			xData.append("'"+dateStr+"',");
			
			List<LightStatusRecord> recordList = data.get(dateStr);
			int batteryVol = 0;
			for(LightStatusRecord record : recordList) {
				batteryVol +=record.getBatteryVol();
			}
			
			yData.append(getBatteryVolLevel(batteryVol/recordList.size())+",");
		}
		
		xData.deleteCharAt(xData.length()-1);
		xData.append("]");
		
		yData.deleteCharAt(yData.length()-1);
		yData.append("]");
		
		result[0] = yData.toString();
		result[1] = xData.toString();
		return result;
	}
	
	private int getBatteryVolLevel(int batteryVol) {
		if(batteryVol<=95) {
			// 欠压
			return 0;
		}else if(batteryVol<=101.7) {
			return 10;
		}else if(batteryVol<=103.9) {
			return 20;
		}else if(batteryVol<=105.2) {
			return 30;
		}else if(batteryVol<=106.3) {
			return 40;
		}else if(batteryVol<=107.5) {
			return 50;
		}else if(batteryVol<=109.5) {
			return 60;
		}else if(batteryVol<=112.3) {
			return 70;
		}else if(batteryVol<=115.6) {
			return 80;
		}else if(batteryVol<=119) {
			return 90;
		}else if(batteryVol<=126) {
			return 100;
		}else {
			return 100;
		}
	}
	
	public String[] querySolarPanelVolData(String startTime, String endTime, Long lightId) {
		String[] result = new String[2];
		List<LightStatusRecord> statusList = lightStatusRecordDao.queryLightStatusRecordListByLightId(startTime, endTime, lightId);
		
		if(statusList==null || statusList.size()==0) {
			result[0] = "[0]";
			result[1] = "[\"2019\"]";
			return result;
		}
		
		Map<String, List<LightStatusRecord>> data = new HashMap<String, List<LightStatusRecord>>();
		
		List<String> keyList = new ArrayList<String>();
		// 数据分类
		for(LightStatusRecord status:statusList) {
			String key = DateUtil.timeFormat(status.getCollectTime());
			if(data.containsKey(key)) {
				data.get(key).add(status);
			}else {
				List<LightStatusRecord> recordList = new ArrayList<LightStatusRecord>();
				recordList.add(status);
				data.put(key, recordList);
				keyList.add(key);
			}
		}
		
		StringBuffer xData = new StringBuffer();
		xData.append("[");
		// 提取Y坐标
		StringBuffer yData = new StringBuffer();
		yData.append("[");
		
		
		// 提取X坐标
		for(String dateStr : keyList) {
			xData.append("'"+dateStr+"',");
			
			List<LightStatusRecord> recordList = data.get(dateStr);
			int solarPanelVol = 0;
			for(LightStatusRecord record : recordList) {
				if(record.getSolarPanelVol()>solarPanelVol) {
					solarPanelVol = record.getSolarPanelVol();
				}
			}
			
			yData.append(solarPanelVol/10.0+",");
		}
		
		xData.deleteCharAt(xData.length()-1);
		xData.append("]");
		
		yData.deleteCharAt(yData.length()-1);
		yData.append("]");
		
		result[0] = yData.toString();
		result[1] = xData.toString();
		return result;
	}
	
	public String[] queryChargeCurrentData(String startTime, String endTime, Long lightId) {
		String[] result = new String[2];
		List<LightStatusRecord> statusList = lightStatusRecordDao.queryLightStatusRecordListByLightId(startTime, endTime, lightId);
		
		if(statusList==null || statusList.size()==0) {
			result[0] = "[0]";
			result[1] = "[\"2019\"]";
			return result;
		}
		
		Map<String, List<LightStatusRecord>> data = new HashMap<String, List<LightStatusRecord>>();
		
		List<String> keyList = new ArrayList<String>();
		// 数据分类
		for(LightStatusRecord status:statusList) {
			String key = DateUtil.timeFormat(status.getCollectTime());
			if(data.containsKey(key)) {
				data.get(key).add(status);
			}else {
				List<LightStatusRecord> recordList = new ArrayList<LightStatusRecord>();
				recordList.add(status);
				data.put(key, recordList);
				keyList.add(key);
			}
		}
		
		StringBuffer xData = new StringBuffer();
		xData.append("[");
		// 提取Y坐标
		StringBuffer yData = new StringBuffer();
		yData.append("[");
		// 提取X坐标
		for(String dateStr : keyList) {
			xData.append("'"+dateStr+"',");
			
			List<LightStatusRecord> recordList = data.get(dateStr);
			int chargeCurrent = 0;
			for(LightStatusRecord record : recordList) {
				chargeCurrent +=record.getChargeCurrent();
			}
			
			yData.append(chargeCurrent/recordList.size()+",");
		}
		
		xData.deleteCharAt(xData.length()-1);
		xData.append("]");
		
		yData.deleteCharAt(yData.length()-1);
		yData.append("]");
		
		result[0] = yData.toString();
		result[1] = xData.toString();
		return result;
	}
	
	public String[] queryWorkCurrentData(String startTime, String endTime, Long lightId) {
		String[] result = new String[2];
		List<LightStatusRecord> statusList = lightStatusRecordDao.queryLightStatusRecordListByLightId(startTime, endTime, lightId);
		
		if(statusList==null || statusList.size()==0) {
			result[0] = "[0]";
			result[1] = "[\"2019\"]";
			return result;
		}
		
		Map<String, List<LightStatusRecord>> data = new HashMap<String, List<LightStatusRecord>>();
		
		List<String> keyList = new ArrayList<String>();
		// 数据分类
		for(LightStatusRecord status:statusList) {
			String key = DateUtil.timeFormat(status.getCollectTime());
			if(data.containsKey(key)) {
				data.get(key).add(status);
			}else {
				List<LightStatusRecord> recordList = new ArrayList<LightStatusRecord>();
				recordList.add(status);
				data.put(key, recordList);
				keyList.add(key);
			}
		}
		
		StringBuffer xData = new StringBuffer();
		xData.append("[");
		// 提取Y坐标
		StringBuffer yData = new StringBuffer();
		yData.append("[");
		// 提取X坐标
		for(String dateStr : keyList) {
			xData.append("'"+dateStr+"',");
			
			List<LightStatusRecord> recordList = data.get(dateStr);
			int workCurrent = 0;
			for(LightStatusRecord record : recordList) {
				workCurrent +=record.getWorkCurrent();
			}
			
			yData.append(workCurrent/recordList.size()+",");
		}
		
		xData.deleteCharAt(xData.length()-1);
		xData.append("]");
		
		yData.deleteCharAt(yData.length()-1);
		yData.append("]");
		
		result[0] = yData.toString();
		result[1] = xData.toString();
		return result;
	}
	
	
	public String[] queryChargeCountData(String startTime, String endTime, Long lightId) {
		String[] result = new String[2];
		List<LightStatusRecord> statusList = lightStatusRecordDao.queryLightStatusRecordListByLightId(startTime, endTime, lightId);
		
		if(statusList==null || statusList.size()==0) {
			result[0] = "[0]";
			result[1] = "[\"2019\"]";
			return result;
		}
		
		Map<String, List<LightStatusRecord>> data = new HashMap<String, List<LightStatusRecord>>();
		
		List<String> keyList = new ArrayList<String>();
		// 数据分类
		for(LightStatusRecord status:statusList) {
			String key = DateUtil.timeFormatH(status.getCollectTime());
			if(data.containsKey(key)) {
				data.get(key).add(status);
			}else {
				List<LightStatusRecord> recordList = new ArrayList<LightStatusRecord>();
				recordList.add(status);
				data.put(key, recordList);
				keyList.add(key);
			}
		}
		
		StringBuffer xData = new StringBuffer();
		xData.append("[");
		// 提取Y坐标
		StringBuffer yData = new StringBuffer();
		yData.append("[");
		
		int lastChargeCount = 0;
		// 提取X坐标
		for(String dateStr : keyList) {
			xData.append("'"+dateStr+"',");
			
			List<LightStatusRecord> recordList = data.get(dateStr);
			int chargeCount = 0;
			for(LightStatusRecord record : recordList) {
				chargeCount +=record.getChargeCount();
			}
			
			int tempChargeCount = chargeCount/recordList.size()/10;
			if(lastChargeCount==0) {
				yData.append("0,");
				lastChargeCount = tempChargeCount;
			}else {
				if(tempChargeCount-lastChargeCount>0) {
					yData.append((tempChargeCount-lastChargeCount)+",");
					lastChargeCount = tempChargeCount;
				}else {
					yData.append("0,");
					lastChargeCount = 0;
				}
			}
		}
		
		xData.deleteCharAt(xData.length()-1);
		xData.append("]");
		
		yData.deleteCharAt(yData.length()-1);
		yData.append("]");
		
		result[0] = yData.toString();
		result[1] = xData.toString();
		return result;
	}
	
	public String[] queryWorkCountData(String startTime, String endTime, Long lightId) {
		String[] result = new String[2];
		List<LightStatusRecord> statusList = lightStatusRecordDao.queryLightStatusRecordListByLightId(startTime, endTime, lightId);
		
		if(statusList==null || statusList.size()==0) {
			result[0] = "[0]";
			result[1] = "[\"2019\"]";
			return result;
		}
		
		Map<String, List<LightStatusRecord>> data = new HashMap<String, List<LightStatusRecord>>();
		
		List<String> keyList = new ArrayList<String>();
		// 数据分类
		for(LightStatusRecord status:statusList) {
			String key = DateUtil.timeFormatH(status.getCollectTime());
			if(data.containsKey(key)) {
				data.get(key).add(status);
			}else {
				List<LightStatusRecord> recordList = new ArrayList<LightStatusRecord>();
				recordList.add(status);
				data.put(key, recordList);
				keyList.add(key);
			}
		}
		
		StringBuffer xData = new StringBuffer();
		xData.append("[");
		// 提取Y坐标
		StringBuffer yData = new StringBuffer();
		yData.append("[");
		// 提取X坐标
		int lastWorkCount = 0;
		for(String dateStr : keyList) {
			xData.append("'"+dateStr+"',");
			
			List<LightStatusRecord> recordList = data.get(dateStr);
			int workCount = 0;
			for(LightStatusRecord record : recordList) {
				workCount +=record.getWorkCount();
			}
			
			int tempWorkCount = workCount/recordList.size()/10;
			
			if(lastWorkCount==0) {
				yData.append("0,");
				lastWorkCount = tempWorkCount;
			}else {
				if(tempWorkCount-lastWorkCount>0) {
					yData.append((tempWorkCount-lastWorkCount)+",");
					lastWorkCount = tempWorkCount;
				}else {
					yData.append("0,");
					lastWorkCount = 0;
				}
			}
		}
		
		xData.deleteCharAt(xData.length()-1);
		xData.append("]");
		
		yData.deleteCharAt(yData.length()-1);
		yData.append("]");
		
		result[0] = yData.toString();
		result[1] = xData.toString();
		return result;
	}
	
	public PageForm<LightChargeCountBean> queryLightChargeCountListPage(PageForm<LightChargeCountBean> page) {
		int totalSize = lightChargeCountDao.queryLightChargeCountListCount(page.getConditionMap());
		
		if(totalSize>0) {
			page.setTotalSize(totalSize);
			List<LightChargeCount> lightChargeCountList = lightChargeCountDao.queryLightChargeCountListPage(page.getConditionMap());
			page.setDataList(convertLightChargeCountToLightChargeCountBean(lightChargeCountList));
		}else {
			page.setDataList(new ArrayList<LightChargeCountBean>());
		}
		
		return page;
	}
	
	private List<LightChargeCountBean> convertLightChargeCountToLightChargeCountBean(List<LightChargeCount> lightChargeCountList) {
		if(lightChargeCountList==null || lightChargeCountList.size()==0) {
			return null;
		}
		
		List<LightChargeCountBean> lightChargeCountBeanList = new ArrayList<LightChargeCountBean>();
		
		for(LightChargeCount lightChargeCount : lightChargeCountList) {
			LightChargeCountBean lightChargeCountBean = new LightChargeCountBean();
			lightChargeCountBean.setPanId(lightChargeCount.getPanId());
			lightChargeCountBean.setLightNum(lightChargeCount.getLightNum());
			lightChargeCountBean.setChargeDay(DateUtil.timeShortFormat(lightChargeCount.getChargeDay()));
			lightChargeCountBean.setChargeCount(lightChargeCount.getChargeCount()/10.0 + "");
			
			lightChargeCountBeanList.add(lightChargeCountBean);
		}
		
		return lightChargeCountBeanList;
	}
	
	public PageForm<UserChargeCountBean> queryUserChargeCountListPage(PageForm<UserChargeCountBean> page) {
		int totalSize = chargeCountDao.queryUserChargeCountListCount(page.getConditionMap());
		
		if(totalSize>0) {
			page.setTotalSize(totalSize);
			List<UserChargeCountBean> lightChargeCountList = chargeCountDao.queryUserChargeCountListPage(page.getConditionMap());
			for(UserChargeCountBean bean : lightChargeCountList) {
				// 数据转换
				bean.setChargeCountStr(bean.getChargeCount()/10.0+"");
				DecimalFormat format = new DecimalFormat("0.##");
				bean.setCo2amount(format.format(bean.getChargeCount()*1.11));
			}
			page.setDataList(lightChargeCountList);
		}else {
			page.setDataList(new ArrayList<UserChargeCountBean>());
		}
		
		return page;
	}
}
